//
//  WHTabBarController.swift
//  SwiftWB
//
//  Created by wuheng on 2017/2/8.
//  Copyright © 2017年 wuhen. All rights reserved.


import UIKit

class WHTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        setUpChildViewController()
        
        
        
    }

}

extension WHTabBarController {
    
    
    
    
    func setUpChildViewController() {
        
        let dictArr = [
            ["clsName": "WHHomeController", "title": "首页", "img": ""],
            ["clsName": "WHMessageController", "title": "消息", "img": ""],
            ["clsName": "WHDiscoverController", "title": "发现", "img": ""],
            ["clsName": "WHProfileController", "title": "描述", "img": ""]
        ]
        
        
        var sadf: Int = 33
        var childVcArr = [UIViewController]()
        for dict in dictArr{
            
            childVcArr.append(controllerWithDict(dict: dict))
            
        }
        
        viewControllers = childVcArr
        
        
    }

    private func controllerWithDict(dict: [String:String]) -> UIViewController{
        
        
        guard let clsName = dict["clsName"],
            let title = dict["title"],
            let img = dict["img"],
            //这里是通过反射机制通过字符串去找到类名，因为有命名空间的存在需要拼接
            let cls = NSClassFromString(Bundle.main.nameSpace + "." + clsName) as? UIViewController.Type
            
            
        else {
            
            return UIViewController()
        }
        
        let vc = cls.init()
        
        vc.title = title
        
        let nav = WHNavController(rootViewController: vc)
        
        return nav
        
    }
    
    

}
